/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.dao;

import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;
import org.cw.bean.Establishment;

/**
 *
 * @author SVN - Team
 */
public class EstablishmentDAO {

    private RecordStore rs;

    public EstablishmentDAO() {
        //idx | Value
    }

    public void openRecordStore() {
        try {
            rs = RecordStore.openRecordStore("dbEstablishment", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public boolean write(Establishment est) {
        boolean flag = false;
        byte[] byteData = est.toString().getBytes();
        openRecordStore();
        try {
            rs.addRecord(byteData, 0, byteData.length);
            flag = true;
            System.out.println("Write data....");
        } catch (RecordStoreException ex) {
            flag = false;
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception e) {
            }
        }
        return flag;
    }

    public Vector list() {
        Vector v = new Vector();
        openRecordStore();
        try {
            RecordEnumeration re = rs.enumerateRecords(null, null, true);
            while (re.hasNextElement()) {
                byte[] recordBuffer = re.nextRecord();
                Establishment est = new Establishment();
                est.convertByteToObject(recordBuffer);
                System.out.println(est.toString());
                v.addElement(est);
            }
        } catch (Exception ex) {
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception e) {
            }
        }
        return v;
    }

    public boolean update(Establishment est) {
        byte[] byteData = est.toString().getBytes();
        boolean flag = true;
        try {
            RecordEnumeration re = rs.enumerateRecords(null, null, true);
            while (re.hasNextElement()) {
                byte[] recordBuffer = re.nextRecord();
                int id = re.nextRecordId();
                Establishment e = new Establishment();
                e.convertByteToObject(recordBuffer);
                if (e.getId().equals(est.getId())) {
                    rs.setRecord(id, byteData, 0, byteData.length);
                    break;
                }
            }
        } catch (Exception ex) {
            flag = false;
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception e) {
            }
        }
        return flag;
    }

    public boolean delete(Establishment est) {
        boolean flag = true;
        try {
            RecordEnumeration re = rs.enumerateRecords(null, null, true);
            while (re.hasNextElement()) {
                byte[] recordBuffer = re.nextRecord();
                int id = re.nextRecordId();
                Establishment e = new Establishment();
                e.convertByteToObject(recordBuffer);
                if (e.getId().equals(est.getId())) {
                    rs.deleteRecord(id);
                    break;
                }
            }
        } catch (Exception ex) {
             flag = false;
        }finally {
            try {
                rs.closeRecordStore();
            } catch (Exception e) {
            }
        }
        return flag;
    }
}
